package com.tim.third.demo.printer.zw;

import cn.hutool.core.map.MapBuilder;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;

/**
 * @Description:
 * @Version 1.0, 2021/12/21
 * @Author Tim
 */
public class ZhongwuPrinterTest {
    //查询打印机状态
    public void status() throws IOException {
        String urlEncoded = getReqBody("", "");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/status",
                urlEncoded);
    }

    //设置音量
    public void sound() throws IOException {
    	// 声音大小，支持0,1,2,3
        String urlEncoded = getReqBody("sound", "1");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/sound",
                urlEncoded);
    }

    //收银语音播报
    public void voice() throws IOException {
    	// 语音内容(如：["支付宝到账","8","千","8","百","8","十","元"])
    	// 数组
        String urlEncoded = getReqBody("voice", "[\"支付宝到账\",\"8\",\"千\",\"8\",\"百\",\"8\",\"十\",\"元\"]");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/voice",
                urlEncoded);
    }

    //打印
    public void print() throws IOException {
        String urlEncoded = getReqBody("printdata", "撒地方广东省123");
        
        PrinterUtil.sendPost("http://api.zhongwuyun.com/",
                urlEncoded);
    }

    //设置LOGO
    public void logo() throws IOException {
    	// logo图片的base64编码数据,为空表示取消logo,支持jpg/gif/bmp/png格式,黑白图片尤佳,请不要超过40kb
    	// 图片的base64编码是不包含图片头的，如（data:image/jpg;base64,）
    	String base64Str = "iVBORw0KGgoAAAANSUhEUgAAAGsAAAAoCAYAAAD5cwTOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAKkSURBVHhe7ZaxccMwDAAzmBfJFJnCM2SAeAJ3qpLChVqfmlRxkTuft2BEkJAAEFSk2PIFPhR/Z1MyCeIB0k/PL5vg2MBlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGcJlGWIVWfvuO1zOyzg2dI5dOJ4/w+GNjiVe289w+WrCqxhHYO1up4+f27AX45T4zqnd5u8xBh4jBeOFeORziG8bDl9ivGecfzmryWJBNW0fqJ7857ypm8h6a8IJn8XPIlEFbB65Zi0GHi/EQ4sj7pXIovsq8rKQO8iaqNDKpv4mK85DfgOyJjppEKvHd2qbetw9D9hZmAiR+Fz1acO3kRXH6Zqnrp0pK32PMfMY5vEgnbUjwaYExs+pEqmImqyyKqt077yKY7KWHIMobkh02oP6G0JVLqyNhZKL5wpJyH3urDw2bJT9AVjWWZPQJLGETTPES2TpcYlxuIvJvuai/AGaw3qy4BgaA2SbppvsmoqseYmm7LvyzjpOdkh8X+9iPBnkOFJKjOS5aGf/UYzGfTprkKN1i1LBuNEJimSxKu/XaVOXHLLAePzCb+LckEClexd2FjstAKXAtO77d52lntFYxTRJNVm1ztKTKO9C+H4DWUWiM7h+Wlc8hzn0319zd60siwesB1omHxJQrT5dlgSqvp+jrH4Kl8XeVY/nCF+/iHUQXsZZL+J53LGzuLgxUXJTqfvqMjRZcu5EfAfvsWpnfYxH7rG/Z+vHIJ4KfP6HkEU3NQfcFPx2SJiGJosDCcxz/CpLPQZLMeN9xNeHteS7RJZ8ZqSzapDNT95VyG+y4vNRAvuHODzH5Im12J1Vg69vvrOcdXBZhnBZhnBZhnBZhnBZhnBZhnBZhnBZhnBZhnBZhnBZZtiEH8OsuATr80akAAAAAElFTkSuQmCC";
    	base64Str = "";
    	String urlEncoded = getReqBody("logodata", base64Str);
        System.out.println(urlEncoded);
        if(StringUtils.isNotBlank(base64Str)){
            String encoded = URLEncoder.encode(base64Str, "UTF-8");
            System.out.println(encoded);
            urlEncoded = urlEncoded.replace(base64Str, encoded);
        }
        PrinterUtil.sendPost("http://api.zhongwuyun.com/logo",
                urlEncoded);
    }

    //查询打印状态
    public void printstatus() throws IOException {
    	// 打印单号
        String urlEncoded = getReqBody("dataid", "543270662");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/printstatus",
                urlEncoded);
    }

    //取消单条未打印订单
    public void cancelone() throws IOException {
    	// 打印单号
        String urlEncoded = getReqBody("dataid", "543289785");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/cancelone",
                urlEncoded);
    }

    //清空打印队列
    public void emptyprintqueue() throws IOException {
        String urlEncoded = getReqBody("", "");
        PrinterUtil.sendPost("http://api.zhongwuyun.com/emptyprintqueue",
                urlEncoded);
    }

    public static String getReqBody(String key, String val){
        Map<String, String> map = MapBuilder.<String, String>create()
                .put("appid", "8000751")
//				.put("appsecret", "88e3263b211487b29c0bf83b7cfcc66a")
                .put("deviceid", "12419814")
                .put("devicesecret", "5m5vp541")
                .put("timestamp", System.currentTimeMillis()/1000+"")
                .build();
        if(StringUtils.isNotBlank(key) && StringUtils.isNotBlank(val)){
            map.put(key, val);
        }

        List<String> fields = new ArrayList<>(map.keySet());
        Collections.sort(fields, Comparator.naturalOrder());
        StringBuilder builder = new StringBuilder();
        for(String field : fields){
            if("sign".equals(field)){
                continue;
            }
            String fieldVal = map.get(field);
            builder.append(field).append(fieldVal);
        }
        builder.append("88e3263b211487b29c0bf83b7cfcc66a");
        System.out.println(builder.toString());
        String sign = MD5Utils.encodeToHex(builder.toString());
        map.put("sign", sign);
//		System.out.println(sign);
        StringBuilder urlEncoded = new StringBuilder();
        for(String attr : map.keySet()){
            urlEncoded.append("&").append(attr)
                    .append("=").append(map.get(attr));
        }
//        String jsonStr = JSONUtil.formatJsonStr(JSON.toJSONString(map));
//        System.out.println(jsonStr);

        return urlEncoded.substring(1, urlEncoded.length());
    }

}
